﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Admin_AdminProductsMacUpdate : System.Web.UI.Page
{
    string connectionString;
    private SqlConnection con;
    private SqlDataAdapter da;
    private SqlCommand cmd;
    private DataSet ds;

    string ID, Name, Type, Price, Img;
    string texts, Imagess;
    int No;
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["AppleShopConnectionString"].ToString();
        con = new SqlConnection(connectionString);
        con.Open();

        if (!IsPostBack)
        {
            ID = Request.QueryString["ID"];
            Name = Request.QueryString["Name"];
            Type = Request.QueryString["Type"];
            Price = Request.QueryString["Price"];
            Img = Request.QueryString["Image"];

            txtID.Text = ID;
            txtName.Text = Name;
            txtType.Text = Type;
            txtPrice.Text = Price;
            Image1.ImageUrl = Img;

            bindDataToDropDownMacDetails(ID);
            
            texts = Request.QueryString["texts"];
            Imagess = Request.QueryString["Imagess"];

            txtDetails.Text = texts;
            Image2.ImageUrl = Imagess;
        }

    }
    protected void bindDataToDropDownMacDetails(string ID)
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetAllProductDetailsMac", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@ProductsID", SqlDbType.VarChar).Value = ID;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "details");
            int a = ds.Tables["details"].Rows.Count;
            dropDownSeries.Items.Clear();
            for (int i = 0; i < a; i++)
            {
                dropDownSeries.Items.Add(ds.Tables["details"].Rows[i][0].ToString());
            }

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            con.Close();
        }
    }
    protected void Update(String IDs, string Names, string Types, float Prices, string Imgs)
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        cmd = new SqlCommand("spGetUpdateProductsMac", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@ProductsID", SqlDbType.VarChar).Value = IDs;
        cmd.Parameters.Add("@ProductsName", SqlDbType.VarChar).Value = Names;
        cmd.Parameters.Add("@ProductsPrice", SqlDbType.Float).Value = Prices;
        cmd.Parameters.Add("@ProductsImage", SqlDbType.VarChar).Value = Imgs;
        cmd.ExecuteNonQuery();
        con.Close();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string filename = "";
        bool check = false;
        bool upload = false;
        string IDx, Namex, Typex, Imgx;
        float Pricex;
        IDx = txtID.Text;
        Namex = txtName.Text;
        Typex = txtType.Text;
        Pricex = float.Parse(txtPrice.Text);
        if (FileUpload1.HasFile)
        {
            string[] validate = { "bmp", "jpg", "jpeg", "png", "gif" };
            string ext = Path.GetExtension(FileUpload1.PostedFile.FileName);

            for (int i = 0; i < validate.Length; i++)
            {
                if (ext == "." + validate[i])
                {
                    if (FileUpload1.PostedFile.ContentLength <= 512000)
                    {
                        check = true;
                        upload = true;
                        break;
                    }
                }
            }
            if (check)
            {
                filename = Path.GetFileName(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/IMG/Mac/") + filename);
                lblStatus.Text = "Successfully";

                lblStatus.Visible = true;
            }
            else
            {
                lblStatus.Text = "File must be Images and less than 500kb";
                lblStatus.Visible = true;
            }
        }

        if (check == true)
        {


            Imgx = "~/IMG/Mac/" + filename;
            Update(IDx, Namex, Typex, Pricex, Imgx);
            Image1.ImageUrl = Imgx;
            lblStatus.Text = "Successfully";
            lblStatus.Text = "" + IDx + " " + Namex + " " + Typex + " " + Pricex + " " + Imgx;
        }
        else if (check == false)
        {


            Imgx = Request.QueryString["Image"];
            Update(IDx, Namex, Typex, Pricex, Imgx);
            Image1.ImageUrl = Imgx;
            lblStatus.Text = "Successfully";
        }
    }
    //----------------------------------------------------------
    protected void btnGet_Click(object sender, EventArgs e)
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetProductNo", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@No", SqlDbType.Int).Value = dropDownSeries.Text;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            txtDetailsID.Text = ds.Tables[0].Rows[0][1].ToString();
            txtDetails.Text = ds.Tables[0].Rows[0][3].ToString();
            Image2.ImageUrl = ds.Tables[0].Rows[0][2].ToString();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            con.Close();
        }
    }
    protected void UpdateDetails(int No, string text, string images)
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        cmd = new SqlCommand("spGetUpdateDetailsMac", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@DetailsNo", SqlDbType.Int).Value = No;
        cmd.Parameters.Add("@DetailsText", SqlDbType.VarChar).Value = text;
        cmd.Parameters.Add("@DetailsImage", SqlDbType.VarChar).Value = images;
        cmd.ExecuteNonQuery();
        con.Close();
    }
    protected void btnUpdate2_Click(object sender, EventArgs e)
    {
        string filename = "";
        bool check = false;
        bool upload = false;
        string textx, imagesx;
        int Nox;
        Nox = int.Parse(dropDownSeries.Text);
        textx = txtDetails.Text;
        if (FileUpload2.HasFile)
        {
            string[] validate = { "bmp", "jpg", "jpeg", "png", "gif" };
            string ext = Path.GetExtension(FileUpload2.PostedFile.FileName);

            for (int i = 0; i < validate.Length; i++)
            {
                if (ext == "." + validate[i])
                {
                    if (FileUpload2.PostedFile.ContentLength <= 512000)
                    {
                        check = true;
                        upload = true;
                        break;
                    }
                }
            }
            if (check)
            {
                filename = Path.GetFileName(FileUpload2.FileName);
                FileUpload2.SaveAs(Server.MapPath("~/IMG/mac/") + filename);
                lblStatus0.Text = "Successfully";

                lblStatus0.Visible = true;
            }
            else
            {
                lblStatus0.Text = "File must be Images and less than 500kb";
                lblStatus0.Visible = true;
            }
        }

        if (check == true)
        {


            imagesx = "~/IMG/mac/" + filename;
            UpdateDetails(Nox, textx, imagesx);
            Image2.ImageUrl = imagesx;
            lblStatus0.Text = "Successfully";
            lblStatus0.Text = "" + Nox + " " + textx + " " + imagesx;
        }
        else if (check == false)
        {


            imagesx = Request.QueryString["Image"];
            UpdateDetails(Nox, textx, imagesx);
            Image2.ImageUrl = imagesx;
            lblStatus0.Text = "Successfully";
        }
    }
    protected void DeleteDetails(int No)
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        cmd = new SqlCommand("spGetDeleteDetailsMac", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@No", SqlDbType.VarChar).Value = No;
        cmd.ExecuteNonQuery();
        con.Close();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        DeleteDetails(int.Parse(dropDownSeries.Text));
        bindDataToDropDownMacDetails(Request.QueryString["ID"].ToString());
        lblStatus0.Text = "Delete sucessfully";
    }
}